<%@ page import="org.example.domain.MyCalc" %><%--
  Created by IntelliJ IDEA.
  User: Administration
  Date: 2022/5/25
  Time: 16:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>计算器</title>
</head>
<body>
<form action="" method="post">
    第一个数：<input type="text" value="" name="first" size="25px"/>
    <br/><br/>
    第二个数：<input type="text" value="" name="second" size="25px"/>
    <br/><br/>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="submit" value="+" name="operator" size="3"/> &nbsp;&nbsp;&nbsp;
    <input type="submit" value="-" name="operator" size="3"/> &nbsp;&nbsp;&nbsp;
    <input type="submit" value="*" name="operator" size="3"/> &nbsp;&nbsp;&nbsp;
    <input type="submit" value="/" name="operator" size="3"/> &nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" value="清除"/>
</form>
<br/><br/>
<%
    // ============================简单的计算器============================
    //获取表单中的数据进行运算
    String first = request.getParameter("first");//第一个数
    String second = request.getParameter("second");//第二个数
    String operator = request.getParameter("operator");//运算符
    String result = "";//运算结果
    if (operator != null) {
        if (first != null && first != "" && second != null && second != "") {
            //判断运算符
            if ("+".equals(operator)) {
                result = String.valueOf((Integer.valueOf(first) + Integer.valueOf(second)));
            }
            if ("-".equals(operator)) {
                result = String.valueOf((Integer.valueOf(first) - Integer.valueOf(second)));
            }
            if ("*".equals(operator)) {
                result = String.valueOf((Integer.valueOf(first) * Integer.valueOf(second)));
            }
            if ("/".equals(operator)) {

                if ("0".equals(second)) {
                    result = "除数不能为0";
                } else {
                    result = String.valueOf((double) (Integer.valueOf(first) / (double) Integer.valueOf(second)));
                }
            }
            //定义一个计算器类
            MyCalc myCalc = new MyCalc();
            myCalc.setResult(result);
            if (!myCalc.getResult().equals("") && myCalc.getResult() != null) {
                out.print("<h2 style= 'color: blue'>");
                out.print("计算结果：" + first + operator + second + " = " + myCalc.getResult());
                out.print("<h4>");
            } else {
                out.print("计算错误");
            }
        } else {
            out.print("输入不能为空");
        }
    }
%>
<br/><br/>
</body>
</html>
